package com.gt.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gt.app.common.ColonoscopyEnum;
import com.gt.app.domain.EsColonoscopyCheckInfo;
import com.gt.app.domain.EsColonoscopyCheckSymptom;
import com.gt.app.domain.dto.EsColonoscopyCheckInfoDto;
import com.gt.app.mapper.EsColonoscopyCheckInfoMapper;
import com.gt.app.service.EsColonoscopyCheckInfoService;
import com.gt.app.service.EsColonoscopyCheckSymptomService;
import com.gt.app.util.CommonUtils;
import com.gt.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 肠镜检查相关资料表 服务实现类
 * </p>
 *
 * @author caidesheng
 */
@Slf4j
@Service
public class EsColonoscopyCheckInfoServiceImpl extends ServiceImpl<EsColonoscopyCheckInfoMapper, EsColonoscopyCheckInfo> implements EsColonoscopyCheckInfoService {

    @Autowired
    private EsColonoscopyCheckSymptomService esColonoscopyCheckSymptomService;

    @Override
    @Transactional
    public void add(List<EsColonoscopyCheckInfoDto> esColonoscopyCheckInfoDtoList) {

        if (esColonoscopyCheckInfoDtoList == null ){
            return;
        }

        for (EsColonoscopyCheckInfoDto esColonoscopyCheckInfoDto : esColonoscopyCheckInfoDtoList) {
            if (StringUtils.isNotBlank(esColonoscopyCheckInfoDto.getOrdrowid())){
                this.updateById(esColonoscopyCheckInfoDto);
                LambdaQueryWrapper<EsColonoscopyCheckSymptom> wrapper = new LambdaQueryWrapper<EsColonoscopyCheckSymptom>()
                        .eq(EsColonoscopyCheckSymptom::getCloOrdrowid, esColonoscopyCheckInfoDto.getOrdrowid());
                esColonoscopyCheckSymptomService.remove(wrapper);
                EsColonoscopyCheckInfo esColonoscopyCheckInfo = new EsColonoscopyCheckInfo();
                BeanUtils.copyProperties(esColonoscopyCheckInfoDto, esColonoscopyCheckInfo);
                saveItem(esColonoscopyCheckInfoDto,esColonoscopyCheckInfo);
            }else {
                EsColonoscopyCheckInfo esColonoscopyCheckInfo = new EsColonoscopyCheckInfo();
                BeanUtils.copyProperties(esColonoscopyCheckInfoDto, esColonoscopyCheckInfo);
                this.save(esColonoscopyCheckInfo);
                esColonoscopyCheckInfoDto.setOrdrowid(esColonoscopyCheckInfo.getOrdrowid());
                saveItem(esColonoscopyCheckInfoDto,esColonoscopyCheckInfo);
            }
        }
    }

    private void saveItem(EsColonoscopyCheckInfoDto esColonoscopyCheckInfoDto, EsColonoscopyCheckInfo esColonoscopyCheckInfo) {
        if (CommonUtils.isNotBlank(esColonoscopyCheckInfoDto.getMainSymptomItem())) {
            ArrayList<EsColonoscopyCheckSymptom> esColonoscopyCheckSymptoms = new ArrayList<>();
            for (String item : esColonoscopyCheckInfoDto.getMainSymptomItem()) {
                EsColonoscopyCheckSymptom esColonoscopyCheckSymptom = new EsColonoscopyCheckSymptom();
                esColonoscopyCheckSymptom.setCloOrdrowid(esColonoscopyCheckInfo.getOrdrowid());
                esColonoscopyCheckSymptom.setItem(item);
                esColonoscopyCheckSymptom.setType(ColonoscopyEnum.MAIN_SYMPTOM.getName());
                esColonoscopyCheckSymptoms.add(esColonoscopyCheckSymptom);
            }
            esColonoscopyCheckSymptomService.saveBatch(esColonoscopyCheckSymptoms);
        }

        if (CommonUtils.isNotBlank(esColonoscopyCheckInfoDto.getLesionSiteItem())) {
            ArrayList<EsColonoscopyCheckSymptom> esColonoscopyCheckSymptoms = new ArrayList<>();
            for (String item : esColonoscopyCheckInfoDto.getLesionSiteItem()) {
                EsColonoscopyCheckSymptom esColonoscopyCheckSymptom = new EsColonoscopyCheckSymptom();
                esColonoscopyCheckSymptom.setCloOrdrowid(esColonoscopyCheckInfo.getOrdrowid());
                esColonoscopyCheckSymptom.setItem(item);
                esColonoscopyCheckSymptom.setType(ColonoscopyEnum.LESION_SITE.getName());
                esColonoscopyCheckSymptoms.add(esColonoscopyCheckSymptom);
            }
            esColonoscopyCheckSymptomService.saveBatch(esColonoscopyCheckSymptoms);
        }

        if (CommonUtils.isNotBlank(esColonoscopyCheckInfoDto.getPathologyLesionItem())) {
            ArrayList<EsColonoscopyCheckSymptom> esColonoscopyCheckSymptoms = new ArrayList<>();
            for (String item : esColonoscopyCheckInfoDto.getPathologyLesionItem()) {
                EsColonoscopyCheckSymptom esColonoscopyCheckSymptom = new EsColonoscopyCheckSymptom();
                esColonoscopyCheckSymptom.setCloOrdrowid(esColonoscopyCheckInfo.getOrdrowid());
                esColonoscopyCheckSymptom.setItem(item);
                esColonoscopyCheckSymptom.setType(ColonoscopyEnum.PATHOLOGY_LESION.getName());
                esColonoscopyCheckSymptoms.add(esColonoscopyCheckSymptom);
            }
            esColonoscopyCheckSymptomService.saveBatch(esColonoscopyCheckSymptoms);
        }

        if (CommonUtils.isNotBlank(esColonoscopyCheckInfoDto.getPathologyIncreaseItem())) {
            ArrayList<EsColonoscopyCheckSymptom> esColonoscopyCheckSymptoms = new ArrayList<>();
            for (String item : esColonoscopyCheckInfoDto.getPathologyIncreaseItem()) {
                EsColonoscopyCheckSymptom esColonoscopyCheckSymptom = new EsColonoscopyCheckSymptom();
                esColonoscopyCheckSymptom.setCloOrdrowid(esColonoscopyCheckInfo.getOrdrowid());
                esColonoscopyCheckSymptom.setItem(item);
                esColonoscopyCheckSymptom.setType(ColonoscopyEnum.PATHOLOGY_INCREASE.getName());
                esColonoscopyCheckSymptoms.add(esColonoscopyCheckSymptom);
            }
            esColonoscopyCheckSymptomService.saveBatch(esColonoscopyCheckSymptoms);
        }
    }

    @Override
    public List<EsColonoscopyCheckInfoDto> getColonoscopyCheckInfoById(String checkId) {
        List<EsColonoscopyCheckInfoDto> checkInfoList = new ArrayList<>();
        LambdaQueryWrapper<EsColonoscopyCheckInfo> eq = new LambdaQueryWrapper<EsColonoscopyCheckInfo>().eq(EsColonoscopyCheckInfo::getEsCheckId, checkId);
        List<EsColonoscopyCheckInfo> checkInfos = this.list(eq);
        if (checkInfos == null){
            return null;
        }
        for (EsColonoscopyCheckInfo checkInfo : checkInfos) {
            EsColonoscopyCheckInfoDto esColonoscopyCheckInfoDto = new EsColonoscopyCheckInfoDto();
            BeanUtils.copyProperties(checkInfo, esColonoscopyCheckInfoDto);
            LambdaQueryWrapper<EsColonoscopyCheckSymptom> wapper = new LambdaQueryWrapper<EsColonoscopyCheckSymptom>().eq(EsColonoscopyCheckSymptom::getCloOrdrowid, checkInfo.getOrdrowid());
            List<EsColonoscopyCheckSymptom> list = esColonoscopyCheckSymptomService.list(wapper);

            List<String> mainSymptomItem = new ArrayList<>();
            List<String> lesionSiteItem = new ArrayList<>();
            List<String> pathologyLesionItem = new ArrayList<>();
            List<String> pathologyIncreaseItem = new ArrayList<>();

            for (EsColonoscopyCheckSymptom esColonoscopyCheckSymptom : list) {
                if (ColonoscopyEnum.MAIN_SYMPTOM.getName().equals(esColonoscopyCheckSymptom.getType())) {
                    mainSymptomItem.add(esColonoscopyCheckSymptom.getItem());
                }
                if (ColonoscopyEnum.LESION_SITE.getName().equals(esColonoscopyCheckSymptom.getType())) {
                    lesionSiteItem.add(esColonoscopyCheckSymptom.getItem());
                }
                if (ColonoscopyEnum.PATHOLOGY_LESION.getName().equals(esColonoscopyCheckSymptom.getType())) {
                    pathologyLesionItem.add(esColonoscopyCheckSymptom.getItem());
                }
                if (ColonoscopyEnum.PATHOLOGY_INCREASE.getName().equals(esColonoscopyCheckSymptom.getType())) {
                    pathologyIncreaseItem.add(esColonoscopyCheckSymptom.getItem());
                }
            }
            esColonoscopyCheckInfoDto.setMainSymptomItem(mainSymptomItem);
            esColonoscopyCheckInfoDto.setLesionSiteItem(lesionSiteItem);
            esColonoscopyCheckInfoDto.setPathologyLesionItem(pathologyLesionItem);
            esColonoscopyCheckInfoDto.setPathologyIncreaseItem(pathologyIncreaseItem);
            checkInfoList.add(esColonoscopyCheckInfoDto);
        }
        return checkInfoList;
    }

    @Override
    public void delete(String id) {
        EsColonoscopyCheckInfo checkInfo = this.getById(id);
        if (checkInfo != null){
            this.removeById(id);
            LambdaQueryWrapper<EsColonoscopyCheckSymptom> wrapper = new LambdaQueryWrapper<EsColonoscopyCheckSymptom>()
                    .eq(EsColonoscopyCheckSymptom::getCloOrdrowid, checkInfo.getOrdrowid());
            esColonoscopyCheckSymptomService.remove(wrapper);
        }
    }
}
